/**
 * 闲暇任务
 * 逻辑代码
 */
exports.do_xxrw = function () {
    var count_mz = 0;
    var foo = require('./foo.js');
    if (
        ui.dbmz.isChecked() ||
        ui.ps.isChecked() ||
        ui.wcbt.isChecked() ||
        ui.gdg.isChecked() ||
        ui.hzc.isChecked() ||
        ui.qhjs.isChecked()
    ) {
        if (ui.dbmz.isChecked()) {
            //进入地图后找马
            function do_dbmz() {
                function n_cxzm() {
                    function c_tb() {
                        sleep(500);
                        foo.clk(foo.p_mztb, 5, 0, 'wtf', do_dbmz);
                    }
                    dmz = function () {
                        foo.clk(foo.p_djdh, 200);
                        sleep(500);
                        foo.clk(foo.p_gb);
                        count_mz = count_mz + 1;
                        toastLog('本轮马贼' + count_mz);
                        foo.clk(foo.p_zdzz, Number(storage.get('configure').outTime_dmz), 0, 'wtf', n_cxzm, n_cxzm);
                    };
                    function c_xdt() {
                        foo.zjmzx(() => {
                            foo.clk(foo.p_djxdt, 5, 0, 'wtf', 1);
                        });
                        foo.clk(foo.p_dkxdt, 10, 0, 'wtf', c_xdt, c_tb);
                    }
                    function c_dmz() {
                        foo.clk(foo.p_gb);
                        foo.clk(foo.p_djdh, Number(storage.get('configure').outTime_zmz), 0, 'wtf', n_cxzm, dmz);
                    }
                    c_xdt();
                    c_dmz();
                }

                foo.clk(foo.p_gb);
                foo.clk(foo.p_hd, 5, 0, 'wtf', 1, 1);
                foo.clk(foo.p_xxrw);
                foo.clk(foo.p_dbmz);
                sleep(1000);
                foo.clk(foo.p_mzhd, null, 1);
                sleep(500);
                foo.clk(foo.p_fxbq, null, 1);
                sleep(12000);
                n_cxzm();
            }
            foo.zjmzx(do_dbmz);
        } //夺宝马贼
        if (ui.ps.isChecked()) {
            var i5 = 0;
            var ps_flag = true;
            var thread_ps;
            var timer_ps;
            var time_ps;
            function zdps() {
                thread_ps = threads.start(() => {
                    time_ps = 0;
                    timer_ps = setInterval(() => {
                        time_ps++;
                    }, 1000);
                });
                foo.p_hd_f();
                foo.clk(foo.p_xxrw);
                foo.clk(
                    foo.p_bhps,
                    5,
                    0,
                    'wtf',
                    () => {
                        ps_flag = false;
                    },
                    jixu
                );
                function jixu() {
                    sleep(2000);
                    foo.clk(foo.p_djdh, 60, 0, 'wtf', zdps, paoshang);
                }
            } //找到帮会内接取任务人
            function do_qw(yn) {
                foo.zjmzx(() => {
                    foo.clk(foo.p_rwbq);
                    foo.clk(foo.p_xsl, 6000);
                });
                if (yn) {
                    foo.clk(foo.p_wdwz, 2, 0, 'wtf', () => {
                        do_qw('y');
                    });
                } else {
                    foo.clk(foo.p_mdd, 2, 0, 'wtf', do_qw, 2);
                }
            } //前往跑商目的地
            function jsps() {
                foo.clk(foo.p_qw, 5, 0, 'wtf');
                zcs();
                if (foo.clk(foo.p_cs, 20, 0, 'wtf', null, 1)) {
                    sleep(6000); //出售完自动回帮等待时间
                    foo.zjmzx(() => {
                        jiaosp();
                    });
                    sleep(5000);
                    i5++;
                    toastLog('第' + Number(i5) + '次跑商结束');
                } else {
                    do_qw('y');
                    jsps();
                }
            } //找到目的地，结束跑商
            function paoshang() {
                sleep(2000);
                foo.clk(foo.p_qd, 10, 0, 'wtf');
                sleep(2000);
                foo.clk(foo.p_tg, 10, 0, 'wtf');
                sleep(2000);
                foo.clk(foo.p_qd, 5, 0, 'wtf');
                sleep(5000);
                foo.clk(foo.p_qd, 10, 0, 'wtf'); //接商票位置在镜湖加
                function do_gmsp() {
                    foo.clk(foo.p_tg, 2, 0, 'wtf', () => {
                        foo.zjmzx(() => {
                            foo.clk(foo.p_rwbq, 2, 0, 'wtf', null, 2);
                            foo.clk(foo.p_xsl, 6000);
                        });
                        foo.clk(foo.p_tg, 5, 0, 'wtf');
                    });
                    foo.clk(
                        foo.p_djdh,
                        5,
                        0,
                        'wtf',
                        () => {
                            foo.zjmzx(() => {
                                foo.clk(foo.p_xsl, 2, 0, 'wtf');
                                do_gmsp();
                            });
                        },
                        () => {
                            foo.clk(foo.p_gmsp, 8, 0, 'wtf');
                            foo.clk(
                                foo.p_zd,
                                5,
                                0,
                                'wtf',
                                () => {
                                    foo.zjmzx(() => {
                                        foo.clk(foo.p_xsl);
                                        do_gmsp();
                                    });
                                },
                                () => {
                                    foo.clk(foo.p_zd, null, 2);
                                    foo.clk(foo.p_gm, null, 1);
                                    foo.clk(foo.p_gb);
                                    do_qw();
                                    jsps();
                                }
                            );
                        }
                    );
                }
                do_gmsp();
            } //跑商过程
            function zcs() {
                foo.clk(foo.p_qd, 10, 0, 'wtf');
                foo.clk(
                    foo.p_djdh,
                    10,
                    0,
                    'wtf',
                    () => {
                        console.log((time_ps / 60).toFixed(0) + 'm' + (time_ps % 60) + 's', time_ps >= 720);
                        if (time_ps >= 12 * 60) {
                            do_qw('y');
                            jsps();
                        }
                        zcs();
                    },
                    null
                );
            } //找到出售商人
            jiaosp = function () {
                foo.clk(foo.p_rwbq);
                foo.clk(foo.p_xsl); //卡，不出对话加
                foo.clk(foo.p_tg, 30, 0, 'wtf', jiaosp);
                try {
                    console.log(time_ps);
                    thread_ps.clearInterval(timer_ps);
                    thread_ps.isAlive() ? thread.interrupt() : null;
                } catch (error) {
                    console.log(error);
                }
            }; //网速慢卡屏严重导致交任务商人不会自动弹出对话加
            function do_ps() {
                while (ps_flag) {
                    zdps();
                }
                foo.clk(foo.p_gb);
            } //跑商任务入口
            foo.zjmzx(do_ps);
        } //跑商
        if (ui.wcbt.isChecked()) {
            variables.threads.threads_fqkq = threads.start(() => {
                function fqkq() {
                    foo.clk(
                        foo.p_fqkq,
                        120,
                        0,
                        'wtf',
                        fqkq,
                        () => {
                            foo.clk(foo.p_qd, 5, 0, 'wtf', fqkq, fqkq, 'jsq_fqkq');
                        },
                        'jsq_fqkq'
                    ); //放弃开启藏宝洞箱子
                }
                fqkq();
            });
            function crck() {
                foo.clk(foo.p_bbck, 5, 0, 'wtf', null, function () {
                    foo.clk(foo.p_zdcr, 2, 0, 'wtf');
                    sleep(1000);
                    foo.clk(foo.p_gb);
                });
            } //背包满自动存仓库
            function zcbt() {
                var ym = 0;
                //背包翻页
                function fy() {
                    ym = ym + 1;
                    swipe(1019, 460, 1016, 120, 1500);
                    sleep(1000);
                    if (ym >= 8) {
                        foo.zjmzx(do_wcbt);
                    }
                    foo.clk(foo.p_cbt, 1, 0, 'wtf', fy, zdbsy);
                }
                function zdbsy() {
                    foo.clk(foo.p_sy, 5, 0, 'wtf', fy);
                }
                foo.clk(foo.p_cbt, 3, 0, 'wtf', fy, zdbsy);
            } //在背包里找藏宝图
            function reclk() {
                foo.clk(foo.p_bbml, 2, 0, 'wtf', null, crck);
                foo.clk(
                    foo.p_djsy,
                    Number(storage.get('configure').outTime_wcbt),
                    0,
                    'wtf',
                    () => {
                        try {
                            if (foo.findText('藏宝', [1128, 0, 141, 32], 'c')[0]) {
                                console.log('进入藏宝洞');
                                sleep(40000);
                                do_wcbt();
                            } else {
                                do_wcbt();
                            }
                        } catch (error) {
                            console.log(error);
                            do_wcbt();
                        }
                    },
                    reclk
                );
            } //循环点击使用藏宝图，并检测背包是否已满
            function do_wcbt() {
                foo.clk(foo.p_gb);
                foo.clk(foo.p_bb, 5, 0, 'wtf', null, zcbt);
                reclk();
            } //入口
            foo.zjmzx(do_wcbt);
            variables.threads.threads_fqkq.interrupt();
        } //挖藏宝图
        if (ui.gdg.isChecked()) {
            function jhll() {
                if (ui.xl_gdg.getSelectedItemPosition() == 0) {
                    function lxgj() {
                        foo.clk(foo.p_rwbq);
                        foo.clk(foo.p_hdbq, 2, 0, 'wtf', null, () => {
                            foo.clk(foo.p_lxgj, 2, 0, 'wtf', lxgj, () => {
                                foo.clk(foo.p_qd, 10, 0, 'wtf', lxgj);
                            });
                        });
                    }
                    function zhao_jhll() {
                        foo.zjmzx(() => {
                            foo.clk(foo.p_hd);
                        });
                        foo.clk(foo.p_bzrw);
                        foo.clk(foo.p_jhll, 2, 0, 'wtf', zhao_jhll);
                    }
                    zhao_jhll();
                    sleep(1000);
                    if (ui.xl_gj.getSelectedItemPosition() == 0 || ui.xl_gj.getSelectedItemPosition() == 1) {
                        foo.clk(foo.p_dg5c, 2, 0, 'wtf', null, () => {
                            foo.clk(foo.p_dg5c, 2000);
                            foo.clk(foo.p_qd, 10, 0, 'wtf', null, 2);
                        });
                    } else if (ui.xl_gj.getSelectedItemPosition() == 2 || ui.xl_gj.getSelectedItemPosition() == 3) {
                        foo.clk(foo.p_gm5c, 2, 0, 'wtf', null, () => {
                            foo.clk(foo.p_gm5c, 2000);
                            foo.clk(foo.p_qd, 10, 0, 'wtf', null, 2);
                        });
                    }
                    if (ui.xl_gj.getSelectedItemPosition() == 1 || ui.xl_gj.getSelectedItemPosition() == 3) {
                        sleep(5 * 60 * 1000);
                        lxgj();
                    }
                }
                if (ui.xl_gdg.getSelectedItemPosition() == 1) {
                    function do_gdg() {
                        foo.clk(foo.p_zdgs, 5, 0, 'wtf', null, () => {
                            foo.clk(foo.p_qd, 5, 0, 'wtf');
                            sleep(60000);
                            foo.clk(foo.p_zdzz, 2, 0, 'wtf', null, () => {
                                foo.clk(foo.p_zdzz);
                                sleep(600000);
                                do_gdg();
                            });
                        });
                    }
                    do_gdg();
                }
            }
            foo.zjmzx(jhll);
        } //挂地宫
        if (ui.hzc.isChecked()) {
            function hly() {
                foo.clk(foo.p_djxdt, 5, 0, 'wtf', null, 2);
                foo.clk(foo.p_dkxdt, 5, 0, 'wtf');
                foo.clk(foo.p_sjdt, 5, 0, 'wtf', null, 1);
                foo.clk(foo.p_ly, 5, 0, 'wtf', null, 2);
                foo.clk(foo.p_csdbz, 5, 0, 'wtf', null, 9);
            }
            foo.zjmzx(hly);
        } //回主城
        if (ui.qhjs.isChecked()) {
            function j_qhjs() {
                function zcd() {
                    foo.clk(foo.orc_cd, 10, 0, 'wtf', zcd);
                }
                function zsz() {
                    foo.clk(foo.orc_sz, 5, 0, 'wtf', zsz, 1);
                }
                zcd();
                zsz();
                foo.clk(foo.orc_qhjs, 5, 0, 'wtf', null, 1);
                foo.clk(foo.orc_role, 30, 0, 'wtf');
                sleep(2000);
                foo.clk(foo.orc_jr);
                foo.clk(foo.p_qd);
                foo.zjmzx(foo.p_hd);
            }
            foo.zjmzx(j_qhjs);
        } //切换角色
    } else console.log('没有选中执行操作');
    foo.zjmzx(() => {
        toastLog('闲暇任务结束了');
    });
    threads.currentThread().interrupt();
    delete variables.threads.a4;
    layout1.xxrw.setText('开始执行');
};
